<template>
  <div class="searchResult-container">
    <!-- 导航栏 -->
    <van-nav-bar
      :title="`${$route.query.q} 的搜素结果`"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <!-- 上拉列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title"
      @click="$router.push({path:'/article',query:{id:item.art_id}})"
       />
    </van-list>
  </div>
</template>

<script>
// 导入接口
import { getSearchList } from "./home.js";

export default {
  name: "searchResult",
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
    };
  },
  methods: {
    async onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求

      //    1.请求数据
      let res = await getSearchList({
        page: this.page,
        q: this.$route.query.q,
      });
      console.log(res);
      // 2.绑定数据
      this.list.push(...res.data.data.results);
      // 3.页码自增
      this.page++;

      //4.关闭动画， 加载状态结束
      this.loading = false;

      // 5.判断数据全部加载完成
      if (res.data.data.results.length == 0) {
        this.finished = true;
      }
    },
  },
};
</script>

<style lang="less">
.searchResult-container {
  .van-nav-bar__content {
    background-color: #0099fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
    .van-nav-bar__text {
      color: #fff;
    }
  }
}
</style>